<template>
    <div class="app-container">
        <el-form label-width="120px">
            

            <el-form-item label="标题">
                <el-input v-model="banner.title"/>
            </el-form-item>

            <el-form-item label="排序">
                <el-input-number v-model="banner.sort" controls-position="right" :min="0"/>
            </el-form-item>

            <el-form-item label="轮播图">
                <el-upload
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload"
                    :action="BASE_API+'/eduoss/fileoss/upload'"
                    class="avatar-uploader">
                    <img :src="banner.imageUrl">
                </el-upload>
            </el-form-item>

            <el-form-item>
                <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import bannerApi from '@/api/cms/banner'

    export default {
        data() {
            return {
                saveBtnDisabled: false, // 保存按钮是否禁用
                banner: {
                    title: '',
                    sort: 1,
                    imageUrl: '/static/bns.jpg'    //默认图片
                },
                bannerId: '',
                BASE_API: process.env.BASE_API     //接口API地址
            }
        },
        created() {
            //根据路由id，(便于后面查询数据回显)
            if(this.$route.params && this.$route.params.id) {
                this.bannerId = this.$route.params.id
                //调用根据id查询的方法
                this.getInfo()
            }else {
                //初始化
                this.init()
            }
        },
        watch: {   //路由监听
            $route(to, form){  //路由发生变化，方法就会执行
                this.init()
            }
        },
        methods: {
             init() {
                //判断路径中是否有id值
                if(this.$route.params && this.$route.params.id){
                    //从路径中获取id值
                    const bannerId = this.$route.params.id
                    //调用根据id查询的方法
                    this.getBannerById(bannerId)
                }else {
                    //清空表单
                    this.banner = {
                        title: '',
                        sort: 1,
                        imageUrl: '/static/bns.jpg'    //默认图片
                    }
                }
            },
            //根据id查询信息
            getInfo() {
                bannerApi.getBannerById(this.bannerId)
                    .then(response => {
                        this.banner = response.data.item
                    })
            },
            //上传之前调用的方法
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg'
                const isLt2M = file.size / 1024 / 1024 < 2
                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!')
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!')
                }
                return isJPG && isLt2M
            },

            //上传封面成功调用的方法
            handleAvatarSuccess(response, file) {
                this.banner.imageUrl = response.data.url
            },

            //添加课程
            addBanner() {
                bannerApi.addBanner(this.banner)
                    .then(response => {
                        //提示
                        this.$message({
                            type: 'success',
                            message: '添加成功!'
                        })
                         //跳转
                        this.$router.push({path:'/cms/list/'})
                    })
            },

            //修改课程
            updateBanner() {
                bannerApi.updateBanner(this.banner)
                    .then(response => {
                         //提示
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        })
                         //跳转
                        this.$router.push({path:'/cms/list/'})
                    })
            },

            saveOrUpdate() {
                //判断是修改还是添加
                if(!this.bannerId) {
                    this.addBanner()
                }else {
                    this.updateBanner()
                }
            }
        }
    }
</script>